﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using System.IO;
using DevExpress.Utils.Drawing;
using DxChartTestApp2.gallery;


namespace DxChartTestApp2
{
    public partial class Form1 : RibbonForm
    {
        public Form1()
        {
            InitializeComponent();
            InitSkinGallery();

        }
        void InitSkinGallery()
        {
            SkinHelper.InitSkinGallery(rgbiSkins, true);
        }

        private void iNew_ItemClick(object sender, ItemClickEventArgs e)
        {
            //InitLoad..
            String root=@"D:\stockimage\week\80";

            var fileNames = Directory.GetFiles(root);

            Image loadimg = Image.FromFile(@"D:\stockimage\load.png");

            GalleryItemGroup group1 = new GalleryItemGroup();
            group1.Caption = "ALL";
            int c = 0;

            galleryControl.Gallery.ItemImageLayout = ImageLayoutMode.ZoomInside;
            galleryControl.Gallery.ImageSize = new Size(320, 240);
            galleryControl.Gallery.ShowItemText = true;

            foreach (String fileName in fileNames)
            {
                String n=Path.GetFileName(fileName);
                if (n.EndsWith(".jpg") == false)
                    continue;
                int pos=n.IndexOf(".");
                if(pos>0)
                    n=n.Substring(0,pos);

                GalleryItem item = new GalleryItem(Image.FromFile(fileName), n, "abcd");
                /*
                StockImageGalleryItem item = new StockImageGalleryItem();
                item.Caption = n;
                item.setImageFileName(fileName);
                item.Image = loadimg;*/
                group1.Items.Add(item);
                c++;
                
            }
            galleryControl.Gallery.Groups.Add(group1);
        }

        private void repositoryItemTrackBar1_EditValueChanged(object sender, EventArgs e)
        {
     
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            galleryControl.Gallery.ImageSize = new Size(320, 240);
            galleryControl.Refresh();
        }

        private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
            galleryControl.Gallery.ImageSize = new Size(480, 360);
            galleryControl.Refresh();
        }

        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
        {
            galleryControl.Gallery.ImageSize = new Size(640, 480);
            galleryControl.Refresh();
        }

        HashSet<StockImageGalleryItem> items = new HashSet<StockImageGalleryItem>();
        private void galleryControl_Gallery_CustomDrawItemImage(object sender, GalleryItemCustomDrawEventArgs e)
        {
            //
            e.Handled = false;
           /* StockImageGalleryItem item = (StockImageGalleryItem )e.Item;
            if (item.loaded == false)
            {
                if (item.loading == false)
                {
                    items.Add(item);
                    timer1.Start();
                    System.Diagnostics.Debug.WriteLine(item.Caption);
                }
            }*/
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (items.Count > 0)
            {
                galleryControl.BeginInit();
                TimeSpan t1 = new TimeSpan(DateTime.Now.Ticks);
                foreach (StockImageGalleryItem item in items)
                {
                    item.init();
                    items.Remove(item);
                    TimeSpan t2 = new TimeSpan(DateTime.Now.Ticks);
                    long ll=t2.Subtract(t1).Ticks;
                    if (ll > 1000)
                        break;
                }
                galleryControl.EndInit();
                if (items.Count == 0)
                    timer1.Stop();
            }
        }

    }
}